/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jauker.badgeview.example;

import ohos.aafwk.ability.Ability;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

import com.jauker.badgeview.example.view.AbstractPageView;
import com.jauker.widget.BadgeView;

public class ListBadgeView extends AbstractPageView {
    public Ability abilitySlice;
    public Context context;
    public Text text;
    private ListContainer mGroupContainer;

    public ListBadgeView(Ability abilitySlice, String name, Color txtColor) {
        super(abilitySlice, name, txtColor);
        this.abilitySlice = abilitySlice;
    }

    public ListBadgeView(Ability abilitySlice, String name, int iconId, int iconIdSelected, Color txtColor) {
        super(abilitySlice, name, iconId, iconIdSelected, txtColor);
    }

    @Override
    public void initView() {
        super.setRootView(loadView());

    }

    private void initViews(ComponentContainer rootView) {
        mGroupContainer = (ListContainer) rootView.findComponentById(ResourceTable.Id_text1);
        mGroupContainer.setItemProvider(new ListAdapte(abilitySlice));
    }


    private Component loadView() {
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(super.getSlice()).
                parse(ResourceTable.Layout_list_row_badge, null, false);
        initViews(layout);
        return layout;
    }

    static class ComponentHolder {
        Text text;
        BadgeView badge;
    }

    private class ListAdapte extends BaseItemProvider {


        private final String[] DATA = Cheeses.sCheeseStrings;
        private LayoutScatter mInflater;
        private Context mContext;

        private ListAdapte(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return DATA.length;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            ComponentHolder holder;
            if (component == null) {
                component = LayoutScatter.getInstance(slice.getContext()).parse(ResourceTable.Layout_list_item, componentContainer, false);

                holder = new ComponentHolder();
                holder.text = (Text) component.findComponentById(ResourceTable.Id_list_component);

                holder.badge = new BadgeView(mContext);
                holder.badge.setTargetView(holder.text);
                holder.badge.setTextSize(40);
                holder.badge.setBackground(12, RgbPalette.parse("#9b2eef"));
                holder.badge.setBadgeGravity(LayoutAlignment.VERTICAL_CENTER | LayoutAlignment.RIGHT);
                holder.badge.setBadgeMargin(0, 0, 8, 0);
                component.setTag(holder);
            } else {
                holder = (ComponentHolder) component.getTag();
            }
            holder.text.setText(DATA[i]);
            holder.badge.setBadgeCount(DATA[i].length());
            return component;
        }

    }

}


